package com.example.wms;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import com.example.classes.DataBase;
import com.example.classes.Item;
import com.example.classes.Member;

import java.util.Date;

/**
 * Activity for adding new Items.
 */
public class AddItemActivity extends Activity
{
    private Date date;
    private boolean looking_for;   //Indicates if it is a lost item(true) or donation(false)
    private boolean isAddingFound;
    private Member owner;
    private Location location;
    private String category = "Misc";
    private DataBase db = new DataBase(this);

    public void onCreate(Bundle savedInstanceState)
    {
        Log.v("WMS-DEBUG", "ADDITEM: Made an AddItemActivity");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_item);
        isAddingFound = getIntent().getBooleanExtra("found",false);
        if (isAddingFound)
        {
            EditText c1 = (EditText) findViewById(R.id.iReward);
            c1.setVisibility(View.INVISIBLE);
            CheckBox c2 = (CheckBox) findViewById(R.id.checkBox);
            c2.setText("This is a donation");
            TextView c3 = (TextView) findViewById(R.id.widget36);
            c3.setVisibility(View.INVISIBLE);
        }

        Intent in = getIntent();
        String user = (in.getStringExtra("username"));

        db = new DataBase(this);
        db.open();
        try{
        owner = db.getEntry(user);
        }
        catch(Exception e)
        {
            Log.d("WMS-DEBUG-ADDITEMACT",e.getMessage());
        }

        date = new Date();
        looking_for = true;

        LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        if (location != null)
            Log.v("WMS-DEBUG", "ADDITEM: The location is: " + location.toString());
        else
        {
            Log.v("WMS-DEBUG", "ADDITEM: The location is FUBAR!");
            location = new Location("Home");
            location.setLatitude(33.65);           //Atlanta coords
            location.setLongitude(84.42);
        }
    }

    /**
     * Creates and adds a new Item to the database.
     * @param view the current view
     */
    public void tryAdd(View view)
    {
        EditText itemName = (EditText)findViewById(R.id.iName);
        String name = itemName.getText().toString();
        EditText itemDesc = (EditText)findViewById(R.id.iDescription);
        String desc = itemDesc.getText().toString();
        EditText itemReward = (EditText)findViewById(R.id.iReward);
        double reward;
        if (isAddingFound)
            reward = 0.0;
        else
        {
            try
            {
                reward = Double.parseDouble(itemReward.getText().toString());
                if (itemReward.getText().toString() == null)
                    reward = 0.0;
            }
            catch (NumberFormatException e)
            {
                reward = 0;
            }
        }

       // EditText itemImageLoc = null;
        String imageLoc = null;

        Log.v("WMS-DEBUG", "ADDITEM: Got the input.");

        Item i = new Item(name, desc, category, location, looking_for, reward, date, imageLoc, owner);
        Intent in=getIntent();
        boolean state=in.getBooleanExtra("found",false);
        i.setState(state);
        Log.v("WMS-DEBUG", "ADDITEM: Made a new Item.");

        owner.addItem(i);
        db.update(owner);
        db.close();
        Log.v("WMS-DEBUG","ADDITEM: Item successfully added to db... Starting Intent");
        Intent gotoItemListView = new Intent(this, itemListViewActivity.class);
        gotoItemListView.putExtra("username", owner.getName());
        gotoItemListView.putExtra("found",state);
        startActivity(gotoItemListView);
        this.finish();
    }

    /**
     * Returns to the previous Activity.
     * @param view the current view
     */
    public void goBack(View view)
    {
        finish();
    }

    /**
     * Sets the category based on the radio buttons.
     * @param view the current view
     */
    public void SetCategory(View view)
    {
        switch(view.getId())
        {
            case R.id.Heirlooms:
                category="Heirloom";
                break;
            case R.id.Keepsakes:
                category="Keepsake";
                break;
            default:
                category="Misc";
        }
    }

    /**
     * Handles checkbox for request/donation
     */
    public void setRequest(View c)
    {
        if (c.getId() == R.id.checkBox)
        {
            final CheckBox chk = (CheckBox)findViewById(R.id.checkBox);
            looking_for = !chk.isChecked();
        }
    }
}